package com.fzy.common.file;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.IOException;
import java.util.*;

/**
 * xml工具类
 *
 * @author 冯赵杨
 * @create 2018/8/26
 */
public class XmlUtil {
    private XmlUtil() {
    }

    /**
     * Map转XML
     *
     * @param map
     * @return
     */

    public static String mapToXML(Map<String, List<Object>> map) throws IOException {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root");
        Element header = root.addElement("header");
        List<Object> head = map.get("header");
        int size = head.size();
        for (int i = 0; i < size; i++) {
            Map<Integer, String> sub = (Map<Integer, String>) head.get(i);

            // 第二种:通过Map.entrySet()使用iterator()遍历key和value
            Iterator<Map.Entry<Integer, String>> iterator = sub.entrySet()
                    .iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, String> entry = iterator.next();

                header.addElement(String.valueOf(entry.getKey())).addText(
                        String.valueOf(entry.getValue()));

            }

            break;
        }

        Element bodys = root.addElement("body");
        List data = map.get("data");
        size = data.size();
        for (int i = 0; i < size; i++) {
            List sub = (List) data.get(i);
            int len = sub.size();
            Element body = bodys.addElement("row");
            for (int j = 0; j < len; j++) {
                body.addElement("col").addText(String.valueOf(sub.get(j)));

            }

        }

        return document.asXML();

    }

    public static Map<String, Object> dom2Map(Document doc) {
        Map<String, Object> map = new HashMap<>();
        if (doc == null) {
            return map;
        }
        Element root = doc.getRootElement();
        for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
            Element e = (Element) iterator.next();
            List list = e.elements();
            if (list.size() > 0) {
                map.put(e.getName(), dom2Map(e));
            } else {
                map.put(e.getName(), e.getText());
            }
        }
        return map;
    }

    public static Map<String, Object> dom2Map(Element e) {
        Map<String, Object> map = new HashMap<>();
        List list = e.elements();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Element iter = (Element) list.get(i);
                List<Object> mapList = new ArrayList<>();

                if (iter.elements().size() > 0) {
                    Map m = dom2Map(iter);
                    if (map.get(iter.getName()) != null) {
                        Object obj = map.get(iter.getName());
                        if (!"java.util.ArrayList".equals(obj.getClass().getName())) {
                            mapList = new ArrayList<>();
                            mapList.add(obj);
                            mapList.add(m);
                        }
                        if ("java.util.ArrayList".equals(obj.getClass().getName())) {
                            mapList = (List<Object>) obj;
                            mapList.add(m);
                        }
                        map.put(iter.getName(), mapList);
                    } else {
                        map.put(iter.getName(), m);
                    }
                } else {
                    if (map.get(iter.getName()) != null) {
                        Object obj = map.get(iter.getName());
                        if (!obj.getClass().getName().equals(
                                "java.util.ArrayList")) {
                            mapList = new ArrayList();
                            mapList.add(obj);
                            mapList.add(iter.getText());
                        }
                        if (obj.getClass().getName().equals(
                                "java.util.ArrayList")) {
                            mapList = (List) obj;
                            mapList.add(iter.getText());
                        }
                        map.put(iter.getName(), mapList);
                    } else {
                        map.put(iter.getName(), iter.getText());
                    }
                }
            }
        } else {
            map.put(e.getName(), e.getText());
        }
        return map;
    }
}
